#ifndef __JGQUEUE_H__
#define __JGQUEUE_H__


#include <queue>
#include <memory>
#include <mutex>

template <class T>
class jgqueue
{
private:
    int _maxlen = 0;
    std::queue<T> _queue;
    std::mutex _lock;
public:
    jgqueue(int maxlen = 10000){
        _maxlen = maxlen;
    };
    ~jgqueue(){};

    void push(T t){
        std::lock_guard<std::mutex> lock(_lock);
        _queue.push(t);
        if (_queue.size() > _maxlen)
        {
            _queue.pop();
        }
        
    };

    bool trypop(T* t){
        std::lock_guard<std::mutex> lock(_lock);
        if (_queue.size() == 0)
        {
            return false;
        }
        *t = _queue.front();
        _queue.pop();
        return true;
    };

    void clear(){
        std::lock_guard<std::mutex> lock(_lock);
        for (size_t i = 0; i < _queue.size(); i++)
        {
            _queue.pop();
        }
    };
};

#endif // __JGQUEUE_H__